package com.sakura.动态规划;

import java.util.Arrays;

public class Code70_爬楼梯 {

    // 暴力递归
    public int climbStairs(int n) {
        if (n < 0) return n;
        if (n == 1 || n == 0) return 1;
        return climbStairs(n - 1) + climbStairs(n - 2);
    }

    // 记忆化搜索
    public int climbStairs1(int n) {
        int[] dp = new int[n + 1];
        Arrays.fill(dp, -1);
        return f1(n, dp);
    }
    public int f1(int n, int[] dp) {
        if (n < 0) return n;
        if (n == 1 || n == 0) return 1;
        if (dp[n] != -1) return dp[n];
        dp[n] = f1(n - 1, dp) + f1(n - 2, dp);
        return dp[n];
    }

    // 自底而上
    public int climbStairs2(int n) {
        int[] dp = new int[n + 1];
        dp[0] = 1;
        dp[1] = 1;
        for (int i = 2; i <= n;i++) {
            dp[i] = dp[i - 1] + dp[i - 2];
        }
        return dp[n];
    }
}
